package cn.fl.ass.controller;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.upload.api.IUploadApiService;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.user.facade.system.api.ISysUsrApiService;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.ass.service.IAssDealCarService;
import cn.fl.ass.vo.AssProFileVO;
import cn.fl.system.constant.SysLogConstant;
import cn.fl.system.service.ISysUlFilesRecService;
import com.alibaba.fastjson.JSON;
import fl.ass.facade.condition.AssProFileQC;
import fl.ass.facade.constant.AssFileConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 处置-变卖信息表 控制层
 */
@Controller
@RequestMapping("/ass/assDealSell")
public class AssDealSellController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(AssDealSellController.class);

	/**
	 * AssDealSellCost 服务层BEAN
	 */
	@Autowired
	private IAssDealCarService assDealCarService;

    /**
     * 附件服务
     */
    @Autowired
    private ISysUlFilesRecService sysUlFilesRecService;

    /**
     * 初始化维护过户方案资料grid
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/initDealFileGrid.do", method = {RequestMethod.GET})
    @ResponseBody
    public Object initDealFileGrid(HttpServletRequest request) {
        String id = request.getParameter("id");
        String cfgCd = request.getParameter("cfgCd");
        // 文件查询条件
        AssProFileQC condition = new AssProFileQC();
        condition.setOptimalId(Long.valueOf(id));
        condition.setOrgCd(AssFileConstant.ASS_PRO_ORG_CD);
        condition.setSysCd(AssFileConstant.ASS_PRO_SYS_CD);
        List<String> cfgCdList = new ArrayList<>();
        cfgCdList.add(cfgCd);
        condition.setCfgCdList(cfgCdList);
        // 查询文件
        List<AssProFileVO> list = this.assDealCarService.searchAssProCfgDtlAndFileCfg(condition);
        if (CollectionUtils.isNotEmpty(list)) {
            return new ListPageVO<AssProFileVO>(list, list.size());
        } else {
            return new ListPageVO<AssProFileVO>();
        }
    }

    /**
     * 在文件接收SYS_UL_FILE_REC表中增加文件上传记录
     *
     * @param request
     */
    @RequestMapping(value = "/uploadDealFile.do", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Object uploadAssProFile(HttpServletRequest request) {
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_FILE_UPLOAD_SUCC);
        String fileParams = request.getParameter("fileParams");
        String cfgCd = request.getParameter("cfgCd");
        String cfgDtlCd = request.getParameter("cfgDtlCd");
        String orgCd = request.getParameter("orgCd");
        String id = request.getParameter("id");
        if (StringUtils.isBlank(id)) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_FILE_UPLOAD_FAIL);
        } else {
            boolean flag = this.sysUlFilesRecService.addAttchmnt(id, fileParams, cfgCd, cfgDtlCd, "ASS_DEAL_SELL", null, null, null, false, orgCd);
            if (!flag) {
                BaseLogUtils.newLogger("上传维护过户方案资料文件失败").setKeys("id:" + id, "cfgCd:" + cfgCd, "cfgDtlCd:" + cfgDtlCd, "fileParams:" + JSON.toJSONString(fileParams)).info();
                result.setSuccess(BaseConstant.IS_YESNO_NO);
                result.setInfo(SysLogConstant.LOG_FILE_UPLOAD_FAIL);
                return result;
            }
        }
        return result;
    }

}

